//
//  TT_TabbarViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/4/1.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_TabbarViewController.h"

@interface TT_TabbarViewController ()<UINavigationControllerDelegate,UITabBarControllerDelegate>

@end

@implementation TT_TabbarViewController

+ (instancetype)shareTabbar {
    
//    static TT_TabbarViewController *shareTab = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        if (!shareTab) {
//            shareTab = [[self alloc] init];
//        }
//    });
//    return shareTab;
    
    static TT_TabbarViewController *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_TabbarViewController new];
    });
    return shareInstance;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    [self uiConfig];
    
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[self imageWithColor:UIColorHex(0xd9d9d9) size:CGSizeMake(TTScreenWidth, 0.5)]];
    
    self.tabBar.translucent = NO;
    [self.tabBar setBarTintColor:[UIColor whiteColor]];
    
}

- (void)setCurrentIndex:(TT_TabbarIndexType)indexType {
    
    [[TT_TabbarViewController shareTabbar] setSelectedIndex:indexType];
}

- (void)uiConfig
{
    _homeNav = [self viewControllerWithTabTitle:@"首页"
                                          image:@"home_nor"
                                  selectedImage:@"home_sel"
                                      className:@"HomeViewController"];

    _categoryNav = [self viewControllerWithTabTitle:@"消息"
                                          image:@"cat_nor"
                                  selectedImage:@"cat_sel"
                                      className:@"MessageViewController"];
    
    _mineNav = [self viewControllerWithTabTitle:@"我的"
                                          image:@"mine_nor"
                                  selectedImage:@"mine_sel"
                                      className:@"MineViewController"];
    
    self.viewControllers = @[_homeNav, _categoryNav, _mineNav];
}

//默认图片设置以及选中状态图片设置
- (TT_NavigationViewController *)viewControllerWithTabTitle:(NSString*)title image:(NSString *)image selectedImage:(NSString *)selectedImage className:(NSString *)className
{
    Class cls = NSClassFromString(className);
    UIViewController *viewController = [[cls alloc] init];
    
    UIImage *Image = [UIImage imageNamed:image];
    UIImage *ImageSel = [UIImage imageNamed:selectedImage];
    
    Image = [Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ImageSel = [ImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:Image selectedImage:ImageSel];

//    viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    
    
    self.tabBar.tintColor= [UIColor redColor];
    
    TT_NavigationViewController *navController = [[TT_NavigationViewController alloc] initWithRootViewController:viewController];

    navController.navigationBar.translucent = NO;
    navController.navigationBar.barTintColor = [UIColor whiteColor];
//    navController.delegate = self;
    return navController;

}

//根据颜色生成图片的函数

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, color.CGColor);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}



@end
